import 'dart:math';

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text("SingleChildScrollView"),
      ),
      body: HomeScreen(),
    ),
  ));
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  ScrollController _controller;
  GlobalKey _keyBlue = GlobalKey();

  /**
   * PageStorageKey  todo
   * NotificationListener TODO
   */

  @override
  void initState() {
    _controller = new ScrollController(initialScrollOffset: 100);
    _controller.addListener(() {
      print(
          "正在滚动:当前滚动位置${_controller.offset},${_controller.position.pixels},最大滚动距离${_controller.position.maxScrollExtent}");

      if (_keyBlue.currentContext != null) {
        // 获取指定widget 的 postion 和 size
        RenderBox box = _keyBlue.currentContext.findRenderObject();
        box.size;

        print("blue box size height:${box.size.height}width:${box.size.width}");

       final positionBlue = box.localToGlobal(Offset.zero);
        print("blue box position:${positionBlue}");
      }
    });

    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Container(
          color: Colors.pink,
          child: SingleChildScrollView(
            scrollDirection: Axis.vertical,
            //滚动方向
            reverse: false,
            padding: EdgeInsets.all(20),
            controller: _controller,
            child: Column(
              children: <Widget>[
                Container(
                  key: _keyBlue,
                  color: Colors.green,
                  width: double.infinity,
                  height: 500,
                ),
                Container(

                  color: Colors.blue,
                  width: double.infinity,
                  height: 500,
                ),
                Container(
                  color: Colors.red,
                  width: double.infinity,
                  height: 500,
                ),
                Container(
                  color: Colors.yellow,
                  width: double.infinity,
                  height: 500,
                )
              ],
            ),
          ),
        ),
        Positioned(
          bottom: 10,
          left: 10,
          child: RaisedButton(
            child: Text("滚动到指定位置无动画"),
            onPressed: () {
              _controller.jumpTo(Random()
                  .nextInt(_controller.position.maxScrollExtent.floor())
                  .toDouble());
            },
          ),
        ),
        Positioned(
          bottom: 100,
          right: 10,
          child: RaisedButton(
            child: Text("滚动到指定位置带动画"),
            onPressed: () {
              _controller.animateTo(
                  Random()
                      .nextInt(_controller.position.maxScrollExtent.floor())
                      .toDouble(),
                  duration: Duration(milliseconds: 300),
                  curve: Curves.easeIn);
            },
          ),
        ),
      ],
    );
  }
}
